Introduction to Image Data with Python¶

Using matplotlib and OpenCV!

Watch the youtube video here that goes along with this notebook!

Forwarding

Routing

Importing libraries¶

In [20]:
import pandas as pd
import numpy as np

from glob import glob

import cv2
import matplotlib.pylab as plt

plt.style.use('ggplot')

Reading in Images¶

Create a list of cat image files and dog image files respectively¶

In [21]:
cat_files = glob('../input/cat-and-dog/training_set/training_set/cats/*.jpg')
dog_files = glob('../input/cat-and-dog/training_set/training_set/dogs/*.jpg')

Read the cat image files using both plt and cv2 libraries¶

In [22]:
img_mpl = plt.imread(cat_files[20])
img_cv2 = cv2.imread(cat_files[20])
#showing shape of image using both libraries, represented in height, width and channel.
img_mpl.shape, img_cv2.shape
Out[22]:
((232, 350, 3), (232, 350, 3))

Image Array¶

(Height, Width, Channels)

imgpixels

Flatten the array data and demonstrate them with a more visually understandable chart.¶

In [23]:
pd.Series(img_mpl.flatten()).plot(kind='hist',
                                  bins=50,
                                  title='Distribution of Pixel Values')
plt.show()

Display Images¶

Showing the image with matplotlib, grid lines turned off¶

In [24]:
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(img_mpl)
ax.axis('off')
plt.show()

Image Channels¶

Result3

RGB Channels¶

An RGB image has three channels: red, green, and blue. RGB channels roughly follow the color receptors in the human eye, and are used in computer displays and image scanners.

Source: Wikipedia#:~:text=RGB%20images,-An%20RGB%20image&text=If%20the%20RGB%20image%20is,intensities%20between%200%20and%20255.)

Result3

Showing the image in R, G, B channels respectively¶

In [25]:
# Display RGB Channels of our image
fig, axs = plt.subplots(1, 3, figsize=(15, 5))
axs[0].imshow(img_mpl[:,:,0], cmap='Reds')
axs[1].imshow(img_mpl[:,:,1], cmap='Greens')
axs[2].imshow(img_mpl[:,:,2], cmap='Blues')
axs[0].axis('off')
axs[1].axis('off')
axs[2].axis('off')
axs[0].set_title('Red channel')
axs[1].set_title('Green channel')
axs[2].set_title('Blue channel')
plt.show()

Matplotlib vs cv2 Numpy Arrays¶

  • cv2 reads in channels as BGR
  • matplotlib reads in channels as RGB

Comparing the difference in channel read order of matplotlib and cv2¶

In [26]:
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
axs[0].imshow(img_cv2)
axs[1].imshow(img_mpl)
axs[0].axis('off')
axs[1].axis('off')
axs[0].set_title('CV2 Image')
axs[1].set_title('Matplotlib Image')
plt.show()

We can easily convert the cv2 format (BGR) to plt format (RGB).¶

In [27]:
# Converting from BGR to RGB
img_cv2_rgb = cv2.cvtColor(img_cv2, cv2.COLOR_BGR2RGB)
fig, ax = plt.subplots()
ax.imshow(img_cv2_rgb)
ax.axis('off')
plt.show()

Image Manipulation¶

Loading the dog image using matplotlib¶

In [28]:
img = plt.imread(dog_files[4])
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(img)
ax.axis('off')
plt.show()

Convert the image into grayscale¶

In [29]:
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(img_gray, cmap='Greys')
ax.axis('off')
ax.set_title('Grey Image')
plt.show()

Resizing and Scaling¶

Shrinking the image to 1/16 of its original size¶

In [30]:
img_resized = cv2.resize(img, None, fx=0.25, fy=0.25)
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(img_resized)
ax.axis('off')
plt.show()

We can also resize and change its dimension ratio at the same time¶

In [31]:
# Different Size
img_resize = cv2.resize(img, (100, 200))
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(img_resize)
ax.axis('off')
plt.show()

We can upscale the image if we specify what interpolation method we want to use¶

In [32]:
img_resize = cv2.resize(img, (5000, 5000), interpolation = cv2.INTER_CUBIC)
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(img_resize)
ax.axis('off')
plt.show()

CV2 Kernels¶

imgpixels

We can manipulate the image further with cv2 Kernel, like edge detection, sharpen or blur.¶

First let's see what a sharpened image looks like¶

In [33]:
# Sharpen Image
kernel_sharpening = np.array([[-1,-1,-1], 
                              [-1,9,-1], 
                              [-1,-1,-1]])

sharpened = cv2.filter2D(img, -1, kernel_sharpening)

fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(sharpened)
ax.axis('off')
ax.set_title('Sharpened Image')
plt.show()

Here's what a blurred image looks like¶

In [34]:
# Blurring the image
kernel_3x3 = np.ones((3, 3), np.float32) / 9
blurred = cv2.filter2D(img, -1, kernel_3x3)
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(blurred)
ax.axis('off')
ax.set_title('Blurred Image')
plt.show()

Save Image¶

We can save the output image, with imsave in matplotlib, or imwrite in cv2.¶

In [35]:
plt.imsave('mpl_dog.png', blurred)
cv2.imwrite('cv2_dog.png', blurred)
Out[35]:
True

The End¶

Conclusion¶

  1. My Kaggle user name is: Razertip
  2. In this tutorial we learned how to convert images to arrays in Python
  3. First we import the numpy and pandas libraries. Then glob, a function to locate files. Finally our heroes today, matplotlib and cv2.
  4. Then we load our image dataset as always.
  5. We learned how the conversion process works, breaking down the pixels into arrays of numbers.
  6. We learned the different ways of array arrangement of plt and cv2, their respective channel organizing order.
  7. Then we are taught a serie of trick to manipulate the image, including resizing, upscaling, grayscaling, blurring, sharpening etc.

Reference¶

https://towardsdatascience.com/image-data-analysis-using-python-edddfdf128f4

https://www.analyticsvidhya.com/blog/2021/09/a-beginners-guide-to-image-processing-with-opencv-and-python/

https://stackabuse.com/introduction-to-image-processing-in-python-with-opencv/

https://www.section.io/engineering-education/image-preprocessing-in-python/

In [ ]: